package com.zxd.admin.controller;


import cn.hutool.crypto.digest.BCrypt;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hs.common.api.dto.PageBean;
import com.hs.common.api.dto.ResultCode;
import com.hs.common.api.dto.ResultVo;
import com.zxd.admin.entity.Admin;
import com.zxd.admin.service.IAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author Dong
 * @since 2022-07-22
 */
@RestController
@RequestMapping("/admin")
@Slf4j
@Api(tags = "管理员模块")
public class AdminController {

    @Autowired
    private IAdminService adminService;


    @GetMapping("/find")
    @ApiOperation("根据名字查找管理员")
    public ResultVo findAdminByName(String username){
        return adminService.findAdminByName(username);
    }

    @PostMapping("/add")
    @ApiOperation("添加管理员")
//    @PreAuthorize("hasAuthority('sys:role:list')")
    public ResultVo addAdmin(@RequestBody Admin admin){

        String gensalt = BCrypt.gensalt();

        String hashpw = BCrypt.hashpw(admin.getPassword(), gensalt);

        admin.setPassword(hashpw);

        boolean save = adminService.saveOrUpdate(admin);

        if(save){

            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);
    }

    @DeleteMapping("/delete")
    @ApiOperation("删除管理员")
    public ResultVo deleteAdmin(@RequestBody Admin admin){

        QueryWrapper<Admin> qr = new QueryWrapper<>();

        qr.eq("userName",admin.getUsername());

        boolean b = adminService.remove(qr);

        if(b){

            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);

    }

    @PutMapping
    @ApiOperation("修改管理员")
    public ResultVo updateAdmin(@RequestBody Admin admin){

        QueryWrapper<Admin> qr = new QueryWrapper<>();

        qr.eq("userName",admin.getUsername());

        boolean b = adminService.saveOrUpdate(admin,qr);

        if(b){

            return new ResultVo(ResultCode.SUCCESS);
        }
        return new ResultVo(ResultCode.FAILED);

    }

    @GetMapping("list")
    @ApiOperation("分页查询管理员")
    public ResultVo findAdmins(Integer currentPage, Integer pageSize,String userName, String status){

        IPage<Admin> page = new Page<>(currentPage,pageSize);

        QueryWrapper<Admin> qr = new QueryWrapper<>();

        if(!StringUtils.isEmpty(userName)){
            qr.eq("userName",userName);
        }

        if(!StringUtils.isEmpty(status)){
            Integer statusInt  = null;

            statusInt = status.equals("启用")?0:1;

            qr.eq("status",statusInt);
        }

        IPage<Admin> adminIPage = adminService.page(page, qr);

        List<Admin> records = adminIPage.getRecords();

        Long total = adminIPage.getTotal();

        if(!CollectionUtils.isEmpty(records)){

            PageBean<Admin> pageBean = new PageBean<>();

            pageBean.setList(records);

            pageBean.setTotals(total.intValue());

            ResultVo resultVo = new ResultVo(pageBean);

            return resultVo;

        }

        return new ResultVo(ResultCode.FAILED);

    }







}

